package org.example;

public class Test1 {
    //leetcode 135 分发糖果 https://leetcode.cn/problems/candy/description/
    /**
     * 从前往后遍历确定右边评分比左边大的情况，从后往前遍历确定左边比右边大的情况
     */
    public int candy(int[] ratings) {
        int n = ratings.length;
        int[] tmp = new int[n];
        tmp[0] = 1;
        for (int i = 1; i < n; i++) {
            tmp[i] = ratings[i] > ratings[i - 1] ? tmp[i - 1] + 1 : 1;
        }
        for (int i = n - 2; i >= 0; i--) {
            if (ratings[i] > ratings[i + 1]) {
                tmp[i] = Math.max(tmp[i + 1] + 1,tmp[i]);
            }
        }
        int ans = 0;
        for (int x : tmp) {
            ans += x;
        }
        return ans;
    }
}
